package com.example.mobile1931605066wzq0604;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button btnMainStartS;
    private Button btnMainStopS;
    MusicService musicService;
    ServiceConnection serviceConnection;
    private Button btnMainPauseMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        final Intent intent = new Intent(MainActivity.this, MusicService.class);
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                musicService = ((MusicService.ServiceBinder) iBinder).getMusicService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                musicService = null;
            }
        };
        btnMainStartS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startService(intent);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });
        btnMainStopS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                stopService(intent);
                  musicService.playMusic();

            }
        });
        btnMainPauseMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                musicService.pauseMusic();
            }
        });
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        btnMainStartS = (Button) findViewById(R.id.btnMainStartS);
        btnMainStopS = (Button) findViewById(R.id.btnMainStopS);
        btnMainPauseMusic = (Button) findViewById(R.id.btnMainPauseMusic);
    }
}
